********************************************************************************
**	PURPOSE: Run Table 7 with outcome variables winsorized, create Appendix Table 7
**							
**	INPUTS: Long with outcomes.dta
**	
**	OUTPUTS: SLCCU products_winsorized outcomes.xlsx
**				
** 	CREATED/MODIFIED BY: Kayla Wilding, Leah Kim
**
**	DATE CREATED: 
**
**	DATE LAST EDITED: 4/11/2022
********************************************************************************clear
clear all

use "$adta/Long with outcomes.dta", clear
local filename "SLCCU products_winsorized outcomes"
local outcomes savingsbal_meh95 savingsbal_meh99 ihs_savingsbal_meh savcheck_meh95 savcheck_meh99 ihs_savcheck_meh
local loanspec1 post 1.enc#1.post  //Average treatment effect spec: includes (Post, CBL Group*Post)
local loanspec2 1.ltca#1.post 1.mtca#1.post 1.htca#1.post 1.ltca#1.enc#1.post 1.mtca#1.enc#1.post 1.htca#1.enc#1.post
	
local decs 3
local loopcount 1
foreach var in `outcomes' {
	cap drop __*
	areg `var' `loanspec1' if flag_randomized == 1, absorb(surveyid) vce(cluster surveyid)
					local obs = e(N)
					local inds = e(df_a) + 1
					sum `var' if e(sample) == 1 & index == 1 & flag_extragroup ==1
					local mu_lhs = string(r(mean), "%14.`decs'f") 
					outreg2 using "$outputtables/`filename'a.txt", `=cond(`loopcount' == 1,"replace","append")' ///
							keep(1.enc#1.post) sortvar(1.enc#1.post) label nocons noobs dec(`decs') nor2 slow(8000) nonote noaster ///
							addtext(Obs, `obs', Individuals, `inds')


	areg `var' `loanspec2' if flag_randomized == 1 & !mi(ne_ln), absorb(surveyid) vce(cluster surveyid)
					local obs = e(N)
					local inds = e(df_a) + 1
					sum `var' if e(sample) == 1 & index == 1 & flag_extragroup ==1
					local mu_lhs = string(r(mean), "%14.`decs'f") 
					test 1.ltca#1.enc#1.post = 1.mtca#1.enc#1.post 
					local pvalue1_2 = string(r(p), "%14.`decs'f")
					test 1.mtca#1.enc#1.post = 1.htca#1.enc#1.post 
					local pvalue2_3 = string(r(p), "%14.`decs'f")
					test 1.ltca#1.enc#1.post = 1.htca#1.enc#1.post 
					local pvalue1_3 = string(r(p), "%14.`decs'f")
					outreg2 using "$outputtables/`filename'b.txt", `=cond(`loopcount' == 1,"replace","append")' ///
							keep(1.ltca#1.enc#1.post 1.mtca#1.enc#1.post 1.htca#1.enc#1.post) sortvar(1.enc#1.post) label nocons noobs dec(`decs') nor2 ///
							addtext(P-value of ltca * CBL * Post = mtca * CBL * Post, `pvalue1_2',	///
									P-value of mtca * CBL * Post = htca * CBL * Post, `pvalue2_3', ///
									P-value of ltca * CBL * Post = htca * CBL * Post, `pvalue1_3', ///
									Obs, `obs', Individuals, `inds', Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs')  slow(8000) nonote noaster
	local ++loopcount
}


		tempfile temp1
		insheet using "$outputtables/`filename'a.txt", noname clear
		save `temp1'
		tempfile temp2
		insheet using "$outputtables/`filename'b.txt", noname clear
		drop if _n <= 5
		save `temp2'
		use `temp1', clear
		append using `temp2'
		replace v1 = v2 if !mi(v2)
		drop v2
		export excel using "$outputtables/`filename'.xls", replace 	


**EOF**
